//
//  ProgressViewController.swift
//  funcode
//
//  Created by Wan Jiading on 2017/6/12.
//  Copyright © 2017年 WanJiading. All rights reserved.
//

import UIKit
import CircleProgressView

@objc protocol ProgressViewControllerDelegate: NSObjectProtocol {
	@objc optional func backgroundDidClick(_ progressViewController: ProgressViewController)
	@objc optional func progressButtonDidClick(_ progressViewController: ProgressViewController)
}

class ProgressViewController: UIViewController {
	
	@IBOutlet weak var bgImageView: UIImageView!
	@IBOutlet weak var progressView: CircleProgressView!
	@IBOutlet weak var progressLabel: UILabel!
	@IBOutlet weak var progressWidth: NSLayoutConstraint!
	
	var delegate: ProgressViewControllerDelegate?
	
	// 总显示时长
	var totalTime: Double = 5.0
	var currentTime: Int = 0
	
	let repeatTime: Double = 0.05
	
	var progressRate: Double!
	
	override func viewDidLoad() {
		super.viewDidLoad()
		
		setupInitData()
		setupControls()
	}
	
	func setupInitData() {
		progressRate = 1.0 / (totalTime * (1.0 / repeatTime))
	}
	
	func setupControls() {
		
		// 1. 背景
		bgImageView.image = UIImage(contentsOfFile: "")
		bgImageView.isUserInteractionEnabled = true
		let bgImageTapGR = UITapGestureRecognizer(target: self, action: #selector(bgImageViewClick))
		bgImageView.addGestureRecognizer(bgImageTapGR)
		
		// 2. 设置 进度条 与 进度条上的文本
		
		// 更新进度条
		self.progressView.progress = 0.0
		let timer1 = Timer(timeInterval: TimeInterval(repeatTime), target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
		RunLoop.main.add(timer1, forMode: RunLoopMode.defaultRunLoopMode)
		
		// 更新时间文本
		let timer2 = Timer(timeInterval: 1.0, target: self, selector: #selector(updateTimeLabel(_:)), userInfo: nil, repeats: true)
		RunLoop.main.add(timer2, forMode: RunLoopMode.defaultRunLoopMode)
		currentTime = Int(totalTime)
		setTimeLabelTime(currentTime)
		
		let progressViewTapGR = UITapGestureRecognizer(target: self, action: #selector(progressViewClick))
		progressView.addGestureRecognizer(progressViewTapGR)
	}
	
	// MARK: - 点击事件
	
	func progressViewClick() {
//		DebugLog("点击了 进度条")
		
		if (delegate != nil && delegate!.responds(to: #selector(ProgressViewControllerDelegate.progressButtonDidClick(_:)))) {
			delegate!.progressButtonDidClick!(self)
		}
	}
	
	func bgImageViewClick() {
//		DebugLog("点击了 背景图")
		
		if (delegate != nil && delegate!.responds(to: #selector(ProgressViewControllerDelegate.backgroundDidClick(_:)))) {
			delegate!.backgroundDidClick!(self)
		}
	}
	
	// MARK: - 更新进度
	
	func updateProgress(_ timer: Timer) {
		if (self.progressView.progress < 1.0) {
			self.progressView.progress += progressRate
		} else {
			self.progressView.progress = 1.0
			timer.invalidate()
			
			UIView.animate(withDuration: 0.5, animations: { [weak self] in
				self?.progressView.alpha = 0
				}, completion: { [weak self] (finished) in
					self?.progressView.removeFromSuperview()
			})
		}
	}
	
	// MARK: - 更新进度文本
	
	func updateTimeLabel(_ timer: Timer) {
		currentTime = currentTime - 1
		if (currentTime == 0) {
			timer.invalidate()
		}
		setTimeLabelTime(currentTime)
	}
	
	func setTimeLabelTime(_ time: Int) {
		progressLabel.text = "跳过\n\(time)S"
	}

}
